package com.minivision.sdktestdemo.controler.ui.activity;

import android.app.Activity;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;

import com.minivision.sdktestdemo.R;
import com.minivision.sdktestdemo.ui.MusicNavFragment;
import com.minivision.sdktestdemo.ui.MusicPlayActivity;

import java.lang.reflect.Field;

/**
 * Created by Administrator on 2016/6/3.
 */
public class MusicSearchControler {
    private SearchView mSearchView;

    private SearchView.SearchAutoComplete mEdit;

    private Activity mContext;

    private SearchHelper mHelper;

    public MusicSearchControler(Activity context) {
        mContext = context;
        mHelper = (SearchHelper) context;
    }

    public interface SearchHelper {
        void beginSearch();

        void endSearch();

        void doSearch(String text);
    }

    public interface SearchResult{
        void updateSearchResult(String text);
    }

    public void initMenuListener(Menu menu) {
        mContext.getMenuInflater().inflate(R.menu.base_top_menu, menu);
        menu.setGroupVisible(R.id.system_group, true);
        MenuItem menuItem = menu.findItem(R.id.action_search);
        MenuItemCompat.setOnActionExpandListener(menuItem, new MenuItemCompat.OnActionExpandListener() {
            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
                //进入搜索栏时,切换fragment
                mHelper.beginSearch();
                return true;
            }

            @Override
            public boolean onMenuItemActionCollapse(MenuItem item) {
                //退出搜索栏时,切换fragment
                mHelper.endSearch();
                return true;
            }
        });
        initSearchView(menuItem);
    }

    public void initSearchView(MenuItem menuItem) {
        mSearchView = (SearchView) MenuItemCompat.getActionView(menuItem);
        final float density = mContext.getResources().getDisplayMetrics().density;
        mSearchView.setIconifiedByDefault(false);
        mSearchView.setQueryHint(mContext.getResources().getString(R.string.search_local_music));
        final int editViewId = mContext.getResources().getIdentifier("search_src_text", "id", mContext.getPackageName());
        mEdit = (SearchView.SearchAutoComplete) mSearchView.findViewById(editViewId);
        if (mEdit != null) {
            mEdit.setCursorVisible(true);
            try {
                Field color = TextView.class.getDeclaredField("mCursorDrawableRes");
                color.setAccessible(true);
                color.set(mEdit, R.drawable.search_text_cursor);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        mSearchView.setSubmitButtonEnabled(false);
        mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                mHelper.doSearch(newText);
                return true;
            }
        });
    }
}
